//
//  BHFunPublishTypeChoseView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/17.
//

import UIKit
protocol BHFunPublishTypeChoseViewDelegate {
    func didChoseLeft(model: BHCommonSortTypeModel)
    func didChoseRight(model: BHCommonSortTypeModel)
    func didTypeChoseClose()
}

class BHFunPublishTypeChoseView: UIView {
    
    var delegate: BHFunPublishTypeChoseViewDelegate?
    
    
    lazy var shadow_view: UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        return view
    }()

    lazy var content_view: UIView = {
        let view = UIView.init()
        view.backgroundColor = .white
        return view
    }()
    
    
    lazy var leftTable: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.register(UINib.init(nibName: "BHHomeChoseCityCell", bundle: nil), forCellReuseIdentifier: "BHHomeChoseCityCell")
        view.delegate = self
        view.dataSource = self
        view.bounces = false
        return view
    }()
    
    lazy var rightTable: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.register(UINib.init(nibName: "BHHomeChoseCityCell", bundle: nil), forCellReuseIdentifier: "BHHomeChoseCityCell")
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .color_f8f8f8
        view.bounces = false
        return view
    }()
    
    var leftArray: [BHCommonSortTypeModel] = []{
        didSet {
            self.leftTable.reloadData()
            self.rightTable.reloadData()
        }
    }
    
    fileprivate var leftIndex: Int = 0
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isHidden = true
        
        self.addSubview(self.shadow_view)
        self.addSubview(self.content_view)
        self.content_view.addSubview(self.leftTable)
        self.content_view.addSubview(self.rightTable)
        
        self.shadow_view.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
        self.content_view.frame = CGRect(x: 0, y: UIScreen.height-UIScreen.safeBottomHeight-240, width: UIScreen.width, height: UIScreen.safeBottomHeight+240)
        
        
        self.leftTable.frame = CGRect(x: 0, y: 0, width: 100, height: 240)
        self.rightTable.frame = CGRect(x: 100, y: 0, width: UIScreen.width-100, height: 240)

        
        self.shadow_view.addViewAction {[weak self] (_) in
            self?.showOrHide(show: false)
            self?.delegate?.didTypeChoseClose()
        }
        
        self.content_view.addCorner(conrners: [.topLeft,.topRight], radius: 8)
        self.content_view.transform = CGAffineTransform.init(translationX: 0, y: UIScreen.safeBottomHeight+240)
    }
}
// animation
extension BHFunPublishTypeChoseView {
    func showOrHide(show: Bool) {
        if show == true {
            self.isHidden = false
            UIView.animate(withDuration: 0.25) {
                self.content_view.transform = CGAffineTransform.identity
            }
        }else {
            UIView.animate(withDuration: 0.25) {
                self.content_view.transform = CGAffineTransform.init(translationX: 0, y: UIScreen.safeBottomHeight+240)
            } completion: { (finished) in
                self.isHidden = true
            }
        }
        
    }
   
}


extension BHFunPublishTypeChoseView: UITableViewDataSource {
   
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.leftTable {
            return self.leftArray.count
        }else {
            if self.leftArray.count == 0 {
                return 0
            }
            let model = self.leftArray[self.leftIndex]
            return model.childArray.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BHHomeChoseCityCell", for: indexPath) as! BHHomeChoseCityCell
        if tableView == leftTable {
            let model = self.leftArray[indexPath.row]
            cell.text_label.text = model.text
            cell.canChose = true
            cell.chose = indexPath.row == self.leftIndex
        }else {
            let leftModel = self.leftArray[self.leftIndex]
            let model = leftModel.childArray[indexPath.row]
            cell.text_label.text = model.text
            cell.canChose = false
        }
        return cell
    }
}

extension BHFunPublishTypeChoseView: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView == self.leftTable {
            let model = self.leftArray[indexPath.row]
            if model.childArray.count == 0 {
                self.delegate?.didChoseLeft(model: model)
            }
            self.leftIndex = indexPath.row
            self.leftTable.reloadData()
            self.rightTable.reloadData()
        }else {
            let leftModel = self.leftArray[self.leftIndex]
            let model = leftModel.childArray[indexPath.row]
            self.delegate?.didChoseRight(model: model)
            
            self.showOrHide(show: false)
            self.delegate?.didTypeChoseClose()
        }
    }
}
